#include "tableitemdeleter.h"
#include "ui_tableitemdeleter.h"
#include "common/common.h"
#include "database/databasequery.h"
#include <QCheckBox>

TableItemDeleter::TableItemDeleter(const QString tabName,const QString showName,QWidget *parent) :
    QDialog(parent,Qt::WindowTitleHint|Qt::CustomizeWindowHint),mTabName(tabName),mShowName(showName),
    ui(new Ui::TableItemDeleter)
{
    ui->setupUi(this);
    this->setWindowTitle("删除");
    setAttribute(Qt::WA_DeleteOnClose);
    //this->setWindowFlags(Qt::WindowTitleHint | Qt::CustomizeWindowHint);
    connect(this,SIGNAL(accepted()),this,SIGNAL(updateData()));
    mListWidget = ui->listWidget;
    mTabItemNameVec.clear();
    initWidget();
    this->setGeometry(WINDOW_GEOMETRY_SHIFT(0.75,0.75,0.2));
}

TableItemDeleter::~TableItemDeleter()
{
    delete ui;
}

void TableItemDeleter::initWidget()
{
    QListWidgetItem *itemTitle = new QListWidgetItem("请选择要删除的项:",mListWidget);
    mListWidget->insertItem(1,itemTitle);
    DataBaseQuery query;
    QStringList keylist;
    keylist<<mShowName;
    query.selectTable(mTabName,&keylist);
    QVector<QString> showNameVt;
    while (query.next()) {
        QString showName = query.value(0).toString();
        if(!showNameVt.contains(showName))
        {
            QListWidgetItem *item = new QListWidgetItem(mListWidget);
            showNameVt.append(showName);
            QCheckBox* chkbox = new QCheckBox(showName,this);
            chkbox->setCheckState(Qt::Unchecked);
            item->setSizeHint(QSize(0,30));
            mListWidget->addItem(item);
            mListWidget->setItemWidget(item,chkbox);
            connect(chkbox,SIGNAL(stateChanged(int)),this,SLOT(onStateChanged(int)));
        }

    }

}

void TableItemDeleter::on_btn_selectAll_clicked()
{
    for(int i =1;i<=mListWidget->count()-1;++i)
    {
        QListWidgetItem *item = mListWidget->item(i);
        QCheckBox* cbx =static_cast<QCheckBox*> (mListWidget->itemWidget(item));
        cbx->setCheckState(Qt::Checked);
        QString txt = cbx->text();
        if(!mTabItemNameVec.contains(txt))
            mTabItemNameVec.append(txt);
    }
}

void TableItemDeleter::onStateChanged(int i)
{
    QCheckBox *cbx = (QCheckBox*)(this->sender());
    QString txt = cbx->text();
    if(i==Qt::Checked)
    {
        mTabItemNameVec.append(txt);
    }
    if(i==Qt::Unchecked) {
        int i = mTabItemNameVec.indexOf(txt);
        mTabItemNameVec.remove(i);
    }qDebug()<<mTabItemNameVec.size();
    foreach (auto s, mTabItemNameVec) {
        qDebug()<<s;
    }
}

void TableItemDeleter::on_btn_ok_clicked()
{
    if(mTabItemNameVec.size()<1)
    {
        this->reject();
        return;
    }
    QString option /*= QString("\"%1\" = ").arg(DBTabFormulaInfo::_FORMULA_NAME)*/;
    int i;
    for(i =0;i<mTabItemNameVec.size()-1;++i)
    {
        option += QString("\"%1\" = \"%2\" or ").arg(mShowName).arg(mTabItemNameVec.at(i));
    }
    option += QString("\"%1\" = \"%2\"").arg(mShowName).arg(mTabItemNameVec.at(i));
    qDebug()<<option;
    DataBaseQuery query;
    query.deleteItem(mTabName,option);
    this->accept();
}

void TableItemDeleter::on_btn_cancel_clicked()
{
    this->reject();
}

void TableItemDeleter::on_btn_selectRAll_clicked()
{
    for(int i =1;i<=mListWidget->count()-1;++i)
    {
        QListWidgetItem *item = mListWidget->item(i);
        QCheckBox* cbx =static_cast<QCheckBox*> (mListWidget->itemWidget(item));
        if(cbx->isChecked())
        {
            cbx->setCheckState(Qt::Unchecked);
        }
        else {
            cbx->setCheckState(Qt::Checked);
        }
    }
}
